#include <jni.h>
#include <stdio.h>
#include "IntArray.h"
#define NELEM(x) (sizeof(x)/sizeof(x[0]))

JNIEXPORT jint JNICALL Java_IntArray_sumArray(JNIEnv *env, jobject obj, jintArray arr)
{
	jint i, sum = 0;
#if 0	
	jint buf[10];
	(*env)->GetIntArrayRegion(env, arr, 0, 10, buf);
	for (i = 0; i < 10; i++) {
		sum += buf[i];
	}	
#else
	/*Get/Release<Type>ArrayElements functions*/ 
	jint *carr;
	carr = (*env)->GetIntArrayElements(env, arr, NULL);
	if (NULL == carr) {
		return 0;
	}
	for (i = 0; i < 10; i++) {
		sum += carr[i];
	}	
	(*env)->ReleaseIntArrayElements(env, arr, carr, 0);
#endif
	return sum;
}


/**
 * @brief 在加载动态链接库的时候，JVM会去尝试查找此函数，可以在此函数做一些 native函数的注册
 *
 * @retval JNI的版本
 */
/*
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved){
    printf("I'm in JNI_OnLoad \n");
    //每个java进程只有一个JavaVM
    JNIEnv *env = NULL;
    jint result = -1;
    if (vm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK) {
        return result;
    }
    
    static JNINativeMethod gMethods[] = {
        {
            "get",
            "()Ljava/lang/jint",
            (void*)Java_HelloWorld_get,            
        },
        {
            "set",
            "(Ljava/lang/jint)V",
            (void*)Java_HelloWorld_set,
        }
    };
	jclass clazz = (*env)->FindClass(env, "com/study/jni/HelloWorld");
    (env)->RegisterNatives(env, clazz, gMethods, NELEM(gMethods));
    return JNI_VERSION_1_6;
}
*/

